﻿using System;
using System.Web.UI;
using System.ComponentModel;
using System.Data;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Web;

namespace NewsGroupListWebPart
{
    public class CustomToolPart : Microsoft.SharePoint.WebPartPages.ToolPart
    {
        private string newsgroup;

        // an event handler for the Init event
        private void CustomToolPart_Init(object sender,
            System.EventArgs e)
        {
            newsgroup = "mw.rec.bboard";
        }

        /// <summary>
        /// Constructor for the class. A great place to set Set
        /// default values for additional base class properties
        /// here.
        /// <summary>

        public CustomToolPart()
        {
            // Set default properties
            this.Title = "Newsgroup to display";
            this.Init += new EventHandler(CustomToolPart_Init);
        }
        ///	<summary>
        ///	Called by the tool pane to apply property changes to
        /// the selected Web Part.
        ///	</summary>
        public override void ApplyChanges()
        {
            newsgroup = Page.Request.Form["newsgroup"];
            NewsGroupListWebPart webpart = (NewsGroupListWebPart)this.ParentToolPane.SelectedWebPart;
            // Send the custom text to the Web Part.
            webpart.newsgroup = newsgroup;
            webpart.Title = "Latest posts from " + newsgroup;
            string newsgroupSimple = Regex.Replace(newsgroup, "[^A-Za-z0-9]", "");
            webpart.TitleUrl = "http://" + SPContext.Current.Site.HostName + "/news/Lists/" + newsgroupSimple + "/";
        }

        protected override void RenderToolPart(HtmlTextWriter output)
        {
            try
            {
                using (SPSite site = new SPSite("http://" + SPContext.Current.Site.HostName + "/news"))
                {
                    using (SPWeb web = site.RootWeb)
                    {

                        // Establish a reference to the Web Part.
                        NewsGroupListWebPart webpart = (NewsGroupListWebPart)this.ParentToolPane.SelectedWebPart;

                        output.Write("<select name='newsgroup'>");
                        output.Write("<option value='" + SPEncode.HtmlEncode(webpart.newsgroup) + "' selected='selected'>" + SPEncode.HtmlEncode(webpart.newsgroup) + "</option>");

                        SPList oList = web.GetList("http://" + SPContext.Current.Site.HostName + "/news/Lists/newsgroups");
                        SPListItemCollection oListItems = oList.Items;

                        System.Collections.ArrayList newsgroupList = new System.Collections.ArrayList();

                        foreach (SPListItem item in oListItems)
                        {
                            string groupName = item["Newsgroup"].ToString();
                            if (groupName != webpart.newsgroup)
                            {
                                newsgroupList.Add(groupName);
                            }
                        }

                        newsgroupList.Sort();

                        foreach (string newsgroupName in newsgroupList)
                        {
                            output.Write("<option value='" + newsgroupName + "'>" + newsgroupName + "</option>");
                        }
                        output.Write("</select>");

                    }
                }
            }
            catch
            {
                output.Write("<select name='newsgroup'>");
                output.Write("<option>Can't find sharepoint/news</option>");
                output.Write("</select>");
            }
        }

    }
}